<?php

namespace app\widgets;

use admin\components\AdminUploadAsset;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;

class WebUploadWidget extends InputWidget {
    
    public $uploadOptions = [];
    public $uploadId;

    public $options = [];

    public function run() {
        $this->registerClientScript();

        if ($this->hasModel()) {
            $input = Html::activeHiddenInput($this->model, $this->attribute, $this->options);
        } else {
            $input = Html::hiddenInput($this->name, $this->value, $this->options);
        }
        echo $input;
    }

    protected function registerClientScript() {
        $options = ArrayHelper::merge($this->uploadOptions, $this->options);
        $options = Json::htmlEncode($options);
        $id = $this->uploadId;
        $view = $this->getView();
        AdminUploadAsset::register($view);
        $view->registerJs("\njQuery('#$id').pUpload($options);\n");
    }
}